package cn.com.cennavi.java;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class WriteRedis {

//	static String FileName = "D:\\data\\t_speed_pattern.txt";
	 static String FileName="/APP1/redis_write_20170707/t_speed_pattern.txt";
	static String filePrefix = "congestion_speed_pattern";

	/**
	 * key:congestion_speed_pattern_1_0835 
	 * key1:51494040012
	 * value1:0835_51494040012_1_58.2_4_873_54
	 */
	/**
	 * 
	 * @Description
	 * @author buf
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		JedisCluster jedis = getJedis();
		long i = 0;
		HashMap<String, Map<String, String>> dataMap = new HashMap<String, Map<String, String>>();
		BufferedReader br = new BufferedReader(new FileReader(new File(FileName)));
		String line = br.readLine();
		while ((line = br.readLine()) != null) {
			// System.out.println(i++);
			if (i++ % 1000 == 0) {
				System.out.println(i);
			}
			// WEEKDAY TIME RS_ID RS_KIND TRAVEL_TIME DEGREE RS_LENGTH SPEED
			System.out.println(line);
			String[] arr = line.split("\t");
			String WEEKDAY = arr[0];
			String time = arr[1];
			String rs_id = arr[2];
			String rs_kind = arr[3];
			String travel_time = arr[4];
			String degree = arr[5];
			String rs_length = arr[6];
			String speed = arr[7];

			//
			String key = filePrefix + "_" + WEEKDAY + "_" + time;
			String value1 = time + "_" + rs_id + "_" + degree + "_" + speed + "_" + rs_kind + "_" + rs_length + "_" + travel_time;
			String key1 = rs_id;
//			System.out.println("key:" + key);
//			System.out.println("key1:" + key1);
//			System.out.println("value1:" + value1);
			Map<String, String> v = dataMap.get(key);
			if (v == null) {
				v = new HashMap<String, String>();
				v.put(key1, value1);
				dataMap.put(key, v);
			} else {
				v.put(key1, value1);
			}
		}
		System.out.println("--------------");
		System.out.println(dataMap.size());
		int j = 0;
		for (Map.Entry<String, Map<String, String>> e : dataMap.entrySet()) {
			String key = e.getKey();
			Map<String, String> v = e.getValue();
			jedis.hmset(key, v);
			System.out.println(j++);
		}
		jedis.close();
	}

	public static JedisCluster getJedis() {
		Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
		// Jedis Cluster will attempt to discover cluster nodes automatically
		// 老的redis集群
		// jedisClusterNodes.add(new HostAndPort("172.20.10.141", 9001));

		jedisClusterNodes.add(new HostAndPort("172.20.10.75", 9001));
		JedisCluster jedis = new JedisCluster(jedisClusterNodes);
		return jedis;
	}

}
